/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiu.accesslog.parser.searchengine;

/**
 *
 * @author Hasan Mahmud @date Jun 21, 2012
 */
public enum SearchEngineType
{

        GOOGLE(0),
        BING(1),
        YAHOO(2),
        YANDEX(3),
        BAIDU(4),
        ASKJEEVES(5),
        SEARCH(6),
        SOSO(7),
        SOGOU(8),
        UNKNOWN(9);
        //AOL(10);
        private static String searchEngineNames[] =
        {
                "Google",
                "Bing",
                "Yahoo",
                "Yandex",
                "Baidu",
                "Askjeeves",
                "Search.com",
                "Soso",
                "Sogou",
                "Unknown",
              //  "search.aol"
        };
        private int searchEngineTypeDecimalCode;

        private static SearchEngineType getSearchEngineTypeFromCode(int seTypeCode)
        {
                SearchEngineType seType;
                switch (seTypeCode)
                {
                        case 0:
                                seType = SearchEngineType.GOOGLE;
                                break;

                        case 1:
                                seType = SearchEngineType.BING;
                                break;

                        case 2:
                                seType = SearchEngineType.YAHOO;
                                break;

                        case 3:
                                seType = SearchEngineType.YANDEX;
                                break;

                        case 4:
                                seType = SearchEngineType.BAIDU;
                                break;

                        case 5:
                                seType = SearchEngineType.ASKJEEVES;
                                break;

                        case 6:
                                seType = SearchEngineType.SEARCH;
                                break;

                        case 7:
                                seType = SearchEngineType.SOSO;
                                break;

                        case 8:
                                seType = SearchEngineType.SOGOU;
                                break;

                        case 9:
                                seType = SearchEngineType.UNKNOWN;
                                break;

//                        case 10:
//                                seType = SearchEngineType.AOL;
//                                break;

                        default:
                                seType = SearchEngineType.UNKNOWN;

                }

                return seType;
        }

        private SearchEngineType(int searchEngineTypeCode)
        {
                this.searchEngineTypeDecimalCode = searchEngineTypeCode;
        }

        public String getName()
        {
                return searchEngineNames[searchEngineTypeDecimalCode];

        }

        public static SearchEngineType getSearchEngine(String referrer)
        {

                String referrerLowCase = referrer.toLowerCase();
                int i = 0;
                for (; i < searchEngineNames.length - 1; i++)
                {
                        if (referrerLowCase.indexOf(searchEngineNames[i].toLowerCase()) >= 0)
                        {
                                break;
                        }

                }
                return getSearchEngineTypeFromCode(i);
        }
}
